#ifdef LM35_ENABLE
//logMessage(LOGLEVEL_DEBUG, "LM35", "", "");

unsigned long LM35previousMillis;
float LM35temperature[LM35SENSORS];
float LM35temperatureMin[LM35SENSORS];
float LM35temperatureMax[LM35SENSORS];

/*************************************** 
 * 
 * global functions
 *
 ***************************************/
void setupLM35() {
  logMessage(LOGLEVEL_INFO, "LM35", "setup LM35", "");
  analogReference(INTERNAL1V1);
  initLM35MinMaxValues();
}

void readLM35ValuesDelay() {
  unsigned long tempCurrentMillis = millis();
  long tempDiff = tempCurrentMillis - LM35previousMillis;
  if(tempDiff > LM35READING_DELAY || LM35previousMillis == 0) {    
    LM35previousMillis = tempCurrentMillis;  
    readLM35Values(); 
  }
}

void readLM35Values() {
  float tempValues[LM35SENSORS];
  for (int i=0; i<LM35SENSORS; i++) {
    tempValues[i] = readLM35ValuesAverge(&LM35_dataPin[i]);
  }
  setLM35Temperature(tempValues);
}

float readLM35ValuesAverge(int *aDataPin) {
  float tempTemp = 0.0;
  for (int i=0; i<5; i++) {
    tempTemp = tempTemp + readLM35Temperature(aDataPin);
  }
  return tempTemp/5;
}

float* getLM35Temperature() {
  readLM35ValuesDelay();
  return LM35temperature;
}

float getLM35Temperature(int anIndex) {
  readLM35ValuesDelay();
  return LM35temperature[anIndex];
}

void setLM35Temperature(float aTemperature[LM35SENSORS]) {
  float* tempMinValues = getLM35TemperatureMin();
  float* tempMaxValues = getLM35TemperatureMax();         
  for (int i = 0; i <LM35SENSORS; i++) {
    LM35temperature[i] = aTemperature[i];
    if (LM35temperature[i] > tempMaxValues[i]) {
      tempMaxValues[i] = LM35temperature[i];
    }
    if (LM35temperature[i] < tempMinValues[i]) {
      tempMinValues[i] = LM35temperature[i];
    }        
  }
  setLM35TemperatureMin(tempMinValues);
  setLM35TemperatureMax(tempMaxValues);        
}

float* getLM35TemperatureMin() {
  return LM35temperatureMin;
}

float getLM35TemperatureMin(int anIndex) {
  return LM35temperatureMin[anIndex];
}

void setLM35TemperatureMin(float aTemperature[LM35SENSORS]) {
  for (int i = 0; i <LM35SENSORS; i++) {
    LM35temperatureMin[i] = aTemperature[i];           
  }
}

float* getLM35TemperatureMax() {
  return LM35temperatureMax;
}

float getLM35TemperatureMax(int anIndex) {
  return LM35temperatureMax[anIndex];
}

void setLM35TemperatureMax(float aTemperature[LM35SENSORS]) {
  for (int i = 0; i <LM35SENSORS; i++) {
    LM35temperatureMax[i] = aTemperature[i]; 
  }  
}

void initLM35MinMaxValues() {
  float tempMin[LM35SENSORS];
  float tempMax[LM35SENSORS];  
  for (int i = 0; i < LM35SENSORS; i++) {
    tempMin[i] = 100.0;
    tempMax[i] = 0.0;    
  }
  setLM35TemperatureMin(tempMin);
  setLM35TemperatureMax(tempMax);  
}

void resetLM35MinMax() {
  initLM35MinMaxValues();
}
/*************************************** 
 * 
 * Sensor implementation
 *
 ***************************************/

float readLM35Temperature(int *aDataPin) {
  return analogRead(*aDataPin) / 9.31;
}
#endif

